import 'dart:convert';

import 'package:bot_toast/bot_toast.dart';
import 'package:common_utils/common_utils.dart';
import 'package:exams_app/home_module/exams_module/view.dart';
import 'package:exams_app/net/Address.dart';
import 'package:exams_app/net/DioManager.dart';
import 'package:exams_app/net/persisten_storage.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class LoginModuleLogic extends GetxController {


  Rx<Usesr>model;

  ///发送验证码
  Future requestDataSendCode(String phoneStr)async{

    // Get.dialog(LoadingDialog('发送中...'));

    var prefs = await SharedPreferences.getInstance();

    var url = await prefs.get('urlStr');

    var params = {
      // 'name':'登录确认验证码',
      'mobile':phoneStr,
      'type':'2',
      // 'n':'4'
    };

    String result = await DioUtils(url: url).post('/api/v1/login/0',data:params);
    Map json = jsonDecode(result);
    // Toast.toast(Get.overlayContext,msg: json['message']);




    return json;

  }

  /// 账号登录

  void requestData(String username, String pass) async {


    BotToast.showLoading(clickClose: true);
    // Get.dialog(LoadingDialog('登录中...'));
    // Get.to(home_modulePage());

    var prefs = await SharedPreferences.getInstance();

    var url = await prefs.get('urlStr');

    var params = {
      'login': username,
      'password': pass,
      'type': '0',
    };

    String result = await DioUtils(url: url)
        .post(Address.login, data: params, context: Get.overlayContext);

    var json = jsonDecode(result);

    LogUtil.e('////////////////// ${json}');

    Rx<Usesr> _model = Usesr.fromJson(json).obs;

    model = _model;

    if (_model.value.errmsg == 'ok') {
      // await prefs.setString('acctoken', _model.data.accessToken);

      PersistentStorage().setStorage('acctoken', _model.value.data.accessToken);

      await prefs.setInt('uid', _model.value.data.uid);
      await prefs.setInt('partner_id', _model.value.data.partnerId);
      await prefs.setString('image128', _model.value.data.image128);
      await prefs.setString(
          'job_company_id', _model.value.data.jobCompanyId.toString());
      await prefs.setString('job_user_id', _model.value.data.jobUserId.toString());
      await prefs.setString('name', _model.value.data.name);
      await prefs.setString('companyIdName', _model.value.data.companyIdName);
      await prefs.setString('companyId', _model.value.data.companyId.toString());
      await prefs.setString('full_name', _model.value.data.full_name.toString());

      if(Get.isDialogOpen){
        Get.back();
      }

      BotToast.closeAllLoading();
      Get.off(HomeModulePage());

    } else {
      // Toast.toast(Get.overlayContext, msg: _model.message);
      // Get.back();
      BotToast.showText(text: _model.value.message);
      BotToast.closeAllLoading();
    }

  }


}



class Usesr {
  int errcode;
  String errmsg;
  Data data;
  String message;

  Usesr({this.errcode, this.errmsg, this.data, this.message});

  Usesr.fromJson(Map<String, dynamic> json) {
    errcode = json['errcode'];
    errmsg = json['errmsg'];
    data = json['data'] != null ? new Data.fromJson(json['data']) : null;
    message = json['message'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['errcode'] = this.errcode;
    data['errmsg'] = this.errmsg;
    if (this.data != null) {
      data['data'] = this.data.toJson();
    }
    data['message'] = this.message;
    return data;
  }
}

class Data {
  String locationCompanyId;
  String locationCompanyIdName;
  String accountType;
  String accessToken;
  String login;
  String name;
  String lang;
  String tz;
  var full_name;
  int expiresIn;
  int uid;
  int partnerId;
  int companyId;
  String companyIdName;
  List<int> companyIds;
  String companyIdsName;
  String image128;
  String db;
  String isConfirm;
  var jobUserId;
  var jobCompanyId;

  Data(
      {this.locationCompanyId,
        this.locationCompanyIdName,
        this.accountType,
        this.accessToken,
        this.login,
        this.name,
        this.lang,
        this.tz,
        this.expiresIn,
        this.uid,
        this.partnerId,
        this.companyId,
        this.companyIdName,
        this.companyIds,
        this.companyIdsName,
        this.image128,
        this.db,
        this.full_name,
        this.isConfirm,
        this.jobUserId,
        this.jobCompanyId});

  Data.fromJson(Map<String, dynamic> json) {
    locationCompanyId = json['location_company_id'];
    locationCompanyIdName = json['location_company_id_name'];
    accountType = json['account_type'];
    accessToken = json['access_token'];
    login = json['login'];
    name = json['name'];
    lang = json['lang'];
    full_name = json['full_name'];
    tz = json['tz'];
    expiresIn = json['expires_in'];
    uid = json['uid'];
    partnerId = json['partner_id'];
    companyId = json['company_id'];
    companyIdName = json['company_id_name'];
    companyIds = json['company_ids'].cast<int>();
    companyIdsName = json['company_ids_name'];
    image128 = json['image_128'];
    db = json['db'];
    isConfirm = json['is_confirm'];
    jobUserId = json['job_user_id'];
    jobCompanyId = json['job_company_id'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['location_company_id'] = this.locationCompanyId;
    data['location_company_id_name'] = this.locationCompanyIdName;
    data['account_type'] = this.accountType;
    data['access_token'] = this.accessToken;
    data['login'] = this.login;
    data['name'] = this.name;
    data['lang'] = this.lang;
    data['tz'] = this.tz;
    data['expires_in'] = this.expiresIn;
    data['uid'] = this.uid;
    data['partner_id'] = this.partnerId;
    data['company_id'] = this.companyId;
    data['company_id_name'] = this.companyIdName;
    data['company_ids'] = this.companyIds;
    data['company_ids_name'] = this.companyIdsName;
    data['image_128'] = this.image128;
    data['db'] = this.db;
    data['is_confirm'] = this.isConfirm;
    data['job_user_id'] = this.jobUserId;
    data['job_company_id'] = this.jobCompanyId;
    return data;
  }
}

